/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import logicaNegocio.Administrativo;
import oracle.jdbc.driver.OracleTypes;

/**
 *
 *
 */
public class GestorAdministrativo extends Conexion implements Gestor<Administrativo> {
    private static final String ingresarAdministrativo="{call INSERTAR_FUNCIONARIO(?,?,?}";
    private static final String eliminarAdministrativo="";
    private static final String modificarAdministrativo="";
    private static final String listarAdministrativo="";
    private static final String consultarAdministrativo="{?=call BUSCAR_FUNCIONARIO_ID(?)}";
    private static final String consultarAdministrativoRegistro="";

    @Override
    public void ingresarDato(Administrativo clase) {
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(ingresarAdministrativo);
            cs.setString(1, clase.getIdentificacion());
            cs.setString(2, clase.getNombre());
            cs.setString(3, clase.getDepartamento());
            boolean resultado = cs.execute();
            if(resultado){
                throw null;
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void eliminarDato(String identificacion) {
        CallableStatement ps = null;
        try{
            if(!this.Conectar())
                return;
            ps = conexion.prepareCall(eliminarAdministrativo);
            ps.setString(1, identificacion);
            int resultado = ps.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(ps == null)
                    ps.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void modificarDato(Administrativo clase) {
        CallableStatement cs = null;
        try{
            if(this.Conectar())
                return;
            cs = this.conexion.prepareCall(modificarAdministrativo);
            cs.setString(1, clase.getIdentificacion());
            cs.setString(2, clase.getNombre());
            cs.setString(3, clase.getDepartamento());
            int resultado = cs.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
            if(cs != null)
                cs.close();
            this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public ArrayList<Administrativo> listarDato() {
        ResultSet rs = null;
        ArrayList<Administrativo> coleccion = new ArrayList<Administrativo>();
        Administrativo adm = null;
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return null;
            cs = this.conexion.prepareCall(listarAdministrativo);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                adm = new Administrativo(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getString(4)
                        );
                coleccion.add(adm);
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                this.Desconectar();
                if(coleccion == null || coleccion.isEmpty())
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return coleccion;
    }

    @Override
    public Administrativo consultarDato(String identificacion) {
        ResultSet rs = null;
        Administrativo adm = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarAdministrativo);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, identificacion);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                adm = new Administrativo(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getString(5)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(adm == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return adm;
    }
    
    public Administrativo consultarDatoRegistro(String identificacion) {
        ResultSet rs = null;
        Administrativo adm = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarAdministrativoRegistro);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, identificacion);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                adm = new Administrativo(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getString(5)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(adm == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return adm;
    }
}
